package com.modules.service.sys.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.constant.CommonConstant;
import com.common.vo.Result;
import com.modules.dao.sys.MenuDao;
import com.modules.dao.sys.RoleDao;
import com.modules.dao.sys.UserDao;
import com.modules.dto.sys.resp.MenuDtoResp;
import com.modules.dto.sys.resp.MenuTreeTowResp;
import com.modules.entity.sys.*;
import com.modules.service.sys.DictService;
import com.modules.service.sys.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * service角色
 *
 * @author yyq
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao, RoleEntity> implements RoleService{

    @Resource
    private MenuDao mdao;
    @Resource
    private UserDao udao;
    @Resource
    private DictService dictService;

    
    /**
     * 删除
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> delete(Integer rid) {
    	Integer count = udao.selectCount(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getRid, rid));
        if (count > CommonConstant.ZERO) {
            return Result.fail("请删除角色对应的用户");
        }
        if (rid.equals(CommonConstant.SYS_RID)) {
            return Result.fail("超级管理员不可删除");
        }
		baseMapper.deleteById(rid);
        return Result.success(Result.DELETE_MSG);
    }

    /**
     * 授权菜单
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> saveRoleMenu(Integer rid, String[] mids, Integer loastOid) {
    	RoleMenuEntity roleMenu = new RoleMenuEntity();
		roleMenu.setRid(rid);
		mdao.deleteRoleMenu(roleMenu);
		if(ArrayUtil.isNotEmpty(mids)) {
			List<RoleMenuEntity> list = CollUtil.newArrayList();
			for(String mid : mids) {
				RoleMenuEntity rm = new RoleMenuEntity(null, rid, Integer.parseInt(mid));// oid 没改先设置 1 了
				list.add(rm);
			}
			mdao.insertRoleMenu(list);
		}
        return Result.success("授权成功");
    }

	/**
	 * 角色菜单树形
	 * @param rid
	 * @return
	 */
	@Override
	public Result<?> selectRoleMenuTree(Integer rid) {
		List<MenuDtoResp> mList = mdao.selectRoleMenu(rid);
		List<MenuTreeTowResp> list = getMenuChildren(mList, 0);
		return Result.success(list);
	}

	public List<MenuTreeTowResp> getMenuChildren(List<MenuDtoResp> mList, Integer pid){
		List<MenuTreeTowResp> list = CollUtil.newArrayList();
		for(MenuDtoResp m : mList) {
			if(m.getParentId().equals(pid)) {
				List<MenuTreeTowResp> children = getMenuChildren(mList, m.getMid());
				Boolean checked;
				if(children.size() > CommonConstant.ZERO) { // 这是没有子级的时候才判断是否选中，否则有子级的会子级全选
					checked = false;
				}else{
					checked = m.getRmstate() > CommonConstant.ZERO ? true : false;
				}
				list.add(new MenuTreeTowResp(m.getMid(), m.getMname(), true, checked, children));
			}
		}
		return list;
	}

	/**
	 * 查询授权菜单按钮
	 * @param rid
	 * @return
	 */
	public Result<?> selectRoleMenuOperation(Integer rid, String code) {
		Result<?> result = dictService.getDictSetting(code);
		List<DictSettingEntity> btList = (List<DictSettingEntity>) result.getData();
		List<Map<String, String>> list = mdao.selectRoleMenuOperation(rid, btList);
		return Result.page(list, Long.valueOf(list.size()));
	}

	/**
	 * 保存授权菜单按钮
	 * @param rid
	 * @param list
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public Result<?> saveRoleMenuOperation(Integer rid, List<RoleMenuOperationEntity> list) {
		RoleMenuOperationEntity roleMenu = new RoleMenuOperationEntity();
		roleMenu.setRid(rid);
		mdao.deleteRoleMenuOperation(roleMenu);
		if(CollUtil.isNotEmpty(list)) {
			mdao.insertRoleMenuOperation(list);
		}
		return Result.success();
	}



}
